using System.Collections;
using UnityEngine;

public class Bubble : MonoBehaviour
{
	private CircleCollider2D cCollider;

	private SpriteRenderer cSprite;

	private Rigidbody2D cRigidbody;

	private float targetSize;

	private int mass;

	private Vector3 initPos;

	private Vector3 one = Vector3.one;

	public CircleCollider2D CCollider
	{
		get
		{
			return cCollider;
		}
	}

	public int Mass
	{
		get
		{
			return mass;
		}
		set
		{
			if ((float)value > 0.1f)
			{
				mass = value;
			}
			else
			{
				mass = 0;
			}
			targetSize = ((float)Values.Instance.BubbleAverageMass / 2f + (float)mass) / (float)Values.Instance.BubbleAverageMass;
			base.transform.localScale = targetSize * one;
		}
	}

	public virtual void Awake()
	{
		cCollider = GetComponent<CircleCollider2D>();
		cSprite = GetComponent<SpriteRenderer>();
		cRigidbody = GetComponent<Rigidbody2D>();
		base.transform.localScale = Vector3.one;
	}

	public void EnablePhysics()
	{
		cCollider.enabled = true;
		cRigidbody.simulated = true;
	}

	public void DisablePhysics()
	{
		cCollider.enabled = false;
		cRigidbody.simulated = false;
	}

	public void Initialize(int mass, Vector3 position)
	{
		Mass = mass;
		initPos = position;
		EnablePhysics();
	}

	public void SetPosition()
	{
		cRigidbody.position = initPos;
	}

	public void Finalize()
	{
		DisablePhysics();
		BubblePool.Instance.ReturnBubble(this);
	}

	private void OnTriggerEnter2D(Collider2D col)
	{
		if (col.CompareTag(Values.Instance.CollectibleTag))
		{
			if (col.attachedRigidbody.velocity != Vector2.zero)
			{
				col.attachedRigidbody.velocity *= -1f;
			}
			else
			{
				col.attachedRigidbody.velocity = Random.insideUnitCircle * 2f;
			}
		}
	}

	private IEnumerator ChangeSize()
	{
		while (true)
		{
			if (base.transform.localScale.x != targetSize)
			{
				base.transform.localScale = Vector3.Lerp(base.transform.localScale, targetSize * one, 8f * Time.unscaledDeltaTime);
			}
			yield return new WaitForEndOfFrame();
		}
	}
}
